// component/video-swiper/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    swiperList: {
      type: Array,
      value: [],
      observer: function (n) {
        const { swiperCurrent, _swiperIndex } = this.data;
        this.setData({
          _swiperList: this.getCurrentList(swiperCurrent, _swiperIndex),
        });
      },
    },
    swiperIndex: {
      type: Number,
      value: 0,
      observer: function (n) {
        this.setData({ _swiperIndex: n || 0, swiperCurrent: n % 3 });
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    _swiperList: [],
    _swiperIndex: 0,
    swiperCurrent: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取当前展示的swiperList
    getCurrentList(swiperCurrent = 0, currentIndex = 0) {
      const { swiperList } = this.properties;
      const nextIndex = currentIndex + 1 > swiperList.length - 1 ? 0 : currentIndex + 1;
      const prevIndex = currentIndex - 1 >= 0 ? currentIndex - 1 : swiperList.length - 1;
      let list = [];
      switch (swiperCurrent) {
        case 0:
          list = [currentIndex, nextIndex, prevIndex];
          break;
        case 1:
          list = [prevIndex, currentIndex, nextIndex];
          break;
        case 2:
          list = [nextIndex, prevIndex, currentIndex];
          break;
      }

      return list.map((current, index) => {
        return {
          ...swiperList[current],
          isPlay: index == swiperCurrent,
        };
      });
    },

    handeChange(e) {
      const { current } = e.detail;
      const { swiperCurrent, _swiperIndex } = this.data;
      const { swiperList } = this.properties;
      const isUp = { 0: current == 1, 1: current > 1, 2: current < 1 }[swiperCurrent];

      let index = 0;
      if (isUp) {
        index = _swiperIndex + 1 > swiperList.length - 1 ? 0 : _swiperIndex + 1;
      } else {
        index = _swiperIndex - 1 < 0 ? swiperList.length - 1 : _swiperIndex - 1;
      }

      this.setData({
        _swiperIndex: index,
        _swiperList: this.getCurrentList(current, index),
        swiperCurrent: current,
      });

      this.triggerEvent('change', { currentIndex: index, lastIndex: _swiperIndex });
    },
  },
});
